{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "willing-arrest", "metadata": {}, "outputs": [], "source": [ "# In this Python script we attempt to develop a scheme to import and then\n", "# process data from multiple files.\n", "import numpy as np " ] }, { "cell_type": "code", "execution_count": 2, "id": "composed-roberts", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.210000\n" ] } ], "source": [ "# Before importing multiple files, we first have to learn about some of the\n", "# properties of the '%' operator. The command '%f %n' outputs the floating\n", "# point number n.\n", "print(\"%f\" %3.21)" ] }, { "cell_type": "code", "execution_count": 3, "id": "italic-heaven", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 3.210000\n" ] } ], "source": [ "# If we put a number m in front of the f, it specifies that the number\n", "# output should contain m characters. In the example below, there's lots\n", "# of white space in front of the output.\n", "print(\"%24f\" %3.21)" ] }, { "cell_type": "code", "execution_count": 4, "id": "amazing-peninsula", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "00000000000000003.210000\n" ] } ], "source": [ "# We can force the white space to be filled with zeros by putting a 0\n", "# (zero) in front of the number m.\n", "print(\"%024f\" %3.21)" ] }, { "cell_type": "code", "execution_count": 5, "id": "appointed-sellers", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "00000000000000003.210000\n", "00000000000000321.000000\n" ] } ], "source": [ "# We can also specify the number of characters that should follow the\n", "# decimal point using .p before the f where p is a postive integer.\n", "print(\"%024.6f\" %3.21)\n", "print(\"%024.6f\" %321)" ] }, { "cell_type": "code", "execution_count": 6, "id": "diverse-point", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "000\n", "001\n", "002\n" ] } ], "source": [ "# Suppose our files a labelled something like file001data.txt,\n", "# file002data.txt, ... We can use %f inside a loop to make the 001,\n", "# 002, ... We want our numbers to be m = 3 characters long and to have p =\n", "# 0 characters after the decimal place.\n", "for i in range(3):\n", " print(\"%03.0f\" %i)" ] }, { "cell_type": "code", "execution_count": 7, "id": "diagnostic-variety", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "file000data.txt\n", "file001data.txt\n", "file002data.txt\n" ] } ], "source": [ "# We combine our %f statements with additional text to create the full file\n", "# name.\n", "for i in range(3):\n", " print(\"file%03.0fdata.txt\" %i)" ] }, { "cell_type": "code", "execution_count": 8, "id": "initial-raleigh", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "file000data.txt\n", "file001data.txt\n", "file002data.txt\n" ] } ], "source": [ "# The following acheives exactly the same output but, in my opinion, is a little\n", "# cleaner.\n", "head = \"file\"\n", "toe = \"data.txt\"\n", "for i in range(3):\n", " print(head + \"%03.0f\" %i + toe)" ] }, { "cell_type": "code", "execution_count": 9, "id": "economic-supply", "metadata": {}, "outputs": [], "source": [ "# We now actually load the contents of 5 sequential data files into separate arrays.\n", "dataArray = []\n", "for i in range(1, 6, 1):\n", " dataArray = dataArray + [np.loadtxt(head + \"%03.0f\" %i + toe)]" ] }, { "cell_type": "code", "execution_count": 11, "id": "compact-merit", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The contents of file 1: [1. 1.1 1.2 1.3 1.4]\n", "The contents of file 2: [2. 2.1 2.2 2.3 2.4]\n", "The contents of file 3: [3. 3.1 3.2 3.3 3.4]\n", "The contents of file 4: [4. 4.1 4.2 4.3 4.4]\n", "The contents of file 5: [5. 5.1 5.2 5.3 5.4]\n" ] } ], "source": [ "# We can examine the contents of each array.\n", "for i in range(5):\n", " print('The contents of file ', i + 1, ': ', dataArray[i], sep = '')" ] }, { "cell_type": "code", "execution_count": 13, "id": "amended-database", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[array([1. , 1.1, 1.2, 1.3, 1.4]),\n", " array([3. , 3.1, 3.2, 3.3, 3.4]),\n", " array([5. , 5.1, 5.2, 5.3, 5.4])]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Maybe we only want every second file to be imported. Only a slight\n", "# change to the for loop control statement is required. \n", "oddData = []\n", "for i in range(1, 6, 2):\n", " oddData = oddData + [np.loadtxt(head + \"%03.0f\" %i + toe)]" ] }, { "cell_type": "code", "execution_count": 14, "id": "together-attack", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The contents of every odd file 1: [1. 1.1 1.2 1.3 1.4]\n", "The contents of every odd file 3: [3. 3.1 3.2 3.3 3.4]\n", "The contents of every odd file 5: [5. 5.1 5.2 5.3 5.4]\n" ] } ], "source": [ "# We can examine the contents of each odd numbered array.\n", "for i in range(3):\n", " print('The contents of every odd file ', 2*i + 1, ': ', oddData[i], sep = '')" ] }, { "cell_type": "code", "execution_count": 15, "id": "owned-injury", "metadata": {}, "outputs": [], "source": [ "# Here we import only the even files...\n", "evenData = []\n", "for i in range(2, 6, 2):\n", " evenData = evenData + [np.loadtxt(head + \"%03.0f\" %i + toe)]" ] }, { "cell_type": "code", "execution_count": 16, "id": "daily-mason", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The contents of every even file 2: [2. 2.1 2.2 2.3 2.4]\n", "The contents of every even file 4: [4. 4.1 4.2 4.3 4.4]\n" ] } ], "source": [ "# ... and then examine their contents.\n", "for i in range(2):\n", " print('The contents of every even file ', 2*(i + 1), ': ', evenData[i], sep = '')" ] }, { "cell_type": "code", "execution_count": 17, "id": "activated-luxury", "metadata": {}, "outputs": [], "source": [ "# If you want to select only specific files to be imported using\n", "# non-sequential numbers, you can do the following.\n", "selectedData = []\n", "importList = [1, 2, 3, 4, 5, 8, 12]\n", "for i in range(len(importList)):\n", " selectedData = selectedData + [np.loadtxt(head + \"%03.0f\" %importList[i] + toe)]" ] }, { "cell_type": "code", "execution_count": 18, "id": "higher-graduate", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The contents of each selected file 01: [1. 1.1 1.2 1.3 1.4]\n", "The contents of each selected file 02: [2. 2.1 2.2 2.3 2.4]\n", "The contents of each selected file 03: [3. 3.1 3.2 3.3 3.4]\n", "The contents of each selected file 04: [4. 4.1 4.2 4.3 4.4]\n", "The contents of each selected file 05: [5. 5.1 5.2 5.3 5.4]\n", "The contents of each selected file 08: [8. 8.1 8.2 8.3 8.4]\n", "The contents of each selected file 12: [12. 12.1 12.2 12.3 12.4]\n" ] } ], "source": [ "# The contents of the selected files.\n", "for i in range(len(importList)):\n", " print('The contents of each selected file %02.0f' %importList[i], ': ', selectedData[i], sep = '')" ] }, { "cell_type": "code", "execution_count": 20, "id": "internal-median", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5.0, 5.1000000000000005, 5.2, 5.3, 5.3999999999999995]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Now we can do calculations using the imported data. For example, the\n", "# element-by-element average of the 7 imported files is:\n", "elementAvg = []\n", "for j in range(len(selectedData[0])):\n", " tot = 0\n", " for i in range(len(importList)):\n", " tot += selectedData[i][j]\n", " elementAvg += [tot/len(importList)]\n", "elementAvg" ] }, { "cell_type": "code", "execution_count": 21, "id": "complex-peripheral", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1.2, 2.2, 3.2, 4.2, 5.2, 8.2, 12.2]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Here's the average of each individual file:\n", "fileAvg = []\n", "for i in range(len(importList)):\n", " tot = 0;\n", " for j in range(len(selectedData[0])):\n", " tot += selectedData[i][j]\n", " fileAvg += [tot/len(selectedData[0])]\n", "fileAvg" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }